<?php
namespace App\Service\Admin;

use App\Service\BaseService as Service;
use Illuminate\Support\Facades\DB;

class AdminService extends Service
{
    //管理员

    //列表
    public function getIndex()
    {
        $db = DB::table($this->dbAdmin);;
        $total = $db->count();
        $res = $db->get();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有管理员');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $auth_name = [];
            $authOnes = $this->dbConn($this->dbAuth)
                ->where('del', 0)
                ->where('admin_id', $v['id'])
                ->get();
            if (count($authOnes)) {
                $authList = [];
                foreach ($authOnes as $vv) {
                    $vv = (array) $vv;
                    $action_name = $this->dbConn($this->dbAction)
                        ->where('id', $vv['action_id'])->value('title');
                    if ($action_name) {
                        $authList[] = $action_name;
                    }
                }
                $auth_name = empty($authList) ? '无权限' : implode('，', $authList);
            }
            if ($v['id'] == 1) {
                $auth_name = '包含所有权限';
            }
            $arys[] = [
                'id' => $v['id'],
                'name' => $v['name'],
                'auth_name' => $auth_name,
                'lasttime' => $v['lasttime'],
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys, 'total' => $total);
    }

    //一个
    public function getOne($id)
    {
        $res = (array) DB::table($this->dbAdmin)->where('id', $id)->first();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有管理员');
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $res);
    }

    /**
     * 保存更新
     * @param int $id
     * @param string $title
     */
    public function update($id, $title)
    {
        $res = (array) DB::table($this->dbAdmin)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            $adminData = [
                'name' => $title,
                'password' => md5('123456'),
                'lasttime' => toDate(),
                'addtime' => toDate(),
                'uptime' => toDate(),
            ];
            $up = DB::table($this->dbAdmin)->insertGetId($adminData);
            if (!$up) {
                return array('code' => -2, 'msg' => 'error');
            }
        } else {
            $adminData = [
                'name' => $title,
                'uptime' => toDate(),
            ];
            $up = DB::table($this->dbAdmin)->where('id', $id)->update($adminData);
            if (!$up) {
                return array('code' => -2, 'msg' => 'error');
            }
        }
        return array('code' => 0, 'msg' => 'success');
    }

    /**
     * 保存更新
     * @param int $id
     * @param string $pwd
     * @param string $pwd2
     */
    public function setPwd($id, $pwd, $pwd2)
    {
        $res = (array) DB::table($this->dbAdmin)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return array('code' => -2, 'msg' => '没有管理员');
        }
        if ($res['password'] != md5($pwd)) {
            return array('code' => -2, 'msg' => '密码错误');
        }
        $adminData = [
            'password' => md5($pwd2),
            'uptime' => toDate(),
        ];
        $up = DB::table($this->dbAdmin)->where('id', $id)->update($adminData);
        if (!$up) {
            return array('code' => -2, 'msg' => 'error');
        }
        return array('code' => 0, 'msg' => 'success');
    }

    //权限获取
    public function authList($aid)
    {
        $res = $this->dbConn($this->dbAction)
            ->where('del', 0)
            ->where('pid', 0)
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $authOne = (array) $this->dbConn($this->dbAuth)
                ->where('del', 0)
                ->where('admin_id', $aid)
                ->where('action_id', $v['id'])
                ->first();
            $childs = [];
            $childOnes = $this->dbConn($this->dbAction)
                ->where('del', 0)
                ->where('pid', $v['id'])
                ->get();
            if (count($childOnes)) {
                foreach ($childOnes as $vv) {
                    $vv = (array) $vv;
                    $authSubOne = (array) $this->dbConn($this->dbAuth)
                        ->where('del', 0)
                        ->where('admin_id', $aid)
                        ->where('action_id', $vv['id'])
                        ->first();
                    $childs[] = [
                        'id' => $vv['id'],
                        'title' => $vv['title'],
                        'is_auth' => empty($authSubOne) ? 0 : 1,
                    ];
                }
            }
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'childs' => $childs,
                'is_auth' => empty($authOne) ? 0 : 1,
            ];
        }
        return $this->toback(0, '', $arys);
    }

    /**
     * 权限配置
     * @param int $id 菜单ID
     * @param int $aid 管理员ID
     */
    public function setAuth($id, $aid)
    {
        $res = (array) $this->dbConn($this->dbAuth)
            ->where('del', 0)
            ->where('admin_id', $aid)
            ->where('action_id', $id)
            ->first();
        if (empty($res)) {
            $up = $this->dbConn($this->dbAuth)->insertGetId([
                'admin_id' => $aid, 'action_id' => $id,
                'addtime' => toDate(),
            ]);
            if (!$up) {
                return $this->toback(-1, '添加失败');
            }
        } else {
            $up = $this->dbConn($this->dbAuth)->where('id', $res['id'])->update([
                'del' => 1, 'deltime' => toDate(),
            ]);
            if (!$up) {
                return $this->toback(-1, '删除失败');
            }
        }
        return $this->toback();
    }
}